# -*- coding: utf-8 -*-
#********************************************
# Program must run in virtual env
# (venv)christian@Ubuntu-VirtualBox:/...
#    ^------
#           |     
#            --->Mandatory to run in: virtual env !
#               . venv/bin/activate
#
#     Port Synology
#           
#          Video Station 	5000 (HTTP), 5001 (HTTPS), 9025-9040,
#                               5002, 5004, 65001  
#          Note Station 	5000 (HTTP), 5001 (HTTPS) 	TCP
#     ---> Utilisation 5050
# ********************************************

from flask import Flask
from flask import render_template
from time import sleep

import socket

app = Flask(__name__)

IP_SERVER = '192.168.1.131'

IP_ARDUINO = '192.168.1.30'
TCP_PORT_ARDUINO = 23
BUFFER_SIZE = 80
MSGLEN = 4

@app.route('/')
def home():
  try: 
    # create an INET, STREAMing socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # connect to Socket + add timeout (Rq: connect can throw socket.timeout)
    s.settimeout(5.0)  
    s.connect((IP_ARDUINO, TCP_PORT_ARDUINO))
    # Read Temp + add timeout (Rq: recv can throw socket.timeout)
    s.send(b"RDT\n")
    s.settimeout(5.0)  
    degrev = float(s.recv(BUFFER_SIZE))
    time.sleep(.300)
    # Read Humidity + add timeout (Rq: recv can throw socket.timeout)
    s.send(b"RDH\n")
    s.settimeout(5.0)  
    degreh = float(s.recv(BUFFER_SIZE))
    time.sleep(.300)
    # Read Status + add timeout (Rq: recv can throw socket.timeout)
    s.send(b"RTS\n")
    s.settimeout(5.0)  
    status = float(s.recv(BUFFER_SIZE))
    time.sleep(.300)
    s.close()
  except socket.error:
    degrev = " ----> Socket error occured <---- "
    degreh = "10"
    status = "Unknow"
  #return render_template('home.html', degre=degrev , ipserver=IP_SERVER)
  return render_template('home.html', degre=degrev, humidity=degreh, thermostatus=status, ipserver=IP_SERVER)

@app.route('/on/')
def on(name=None):
  try: 
    # create an INET, STREAMing socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # connect to Socket + add timeout (Rq: connect can throw socket.timeout)
    s.settimeout(5.0)  
    s.connect((IP_ARDUINO, TCP_PORT_ARDUINO))
    # Send command
    s.send(b"CTH\n")
    # Delay Time in seconds.
    sleep(0.5) 
    # Send command
    s.send(b"RDT\n")
    # Read Temp + add timeout (Rq: recv can throw socket.timeout)
    s.settimeout(5.0)  
    degrev = float(s.recv(BUFFER_SIZE))
    s.close()
  except socket.error:
    degrev = " ----> Request ON: Socket error occured <---- "   
  return render_template('home.html', degre=degrev , ipserver=IP_SERVER)

@app.route('/off/')
def off(name=None):
  try: 
    # create an INET, STREAMing socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # connect to Socket + add timeout (Rq: connect can throw socket.timeout)
    s.settimeout(5.0)  
    s.connect((IP_ARDUINO, TCP_PORT_ARDUINO))
    # Send command
    s.send(b"CTL\n")
    # Delay Time in seconds.
    sleep(0.5)
    # Send command
    s.send(b"RDT\n")
    # Read Temp + add timeout (Rq: recv can throw socket.timeout)
    s.settimeout(5.0)  
    degrev = float(s.recv(BUFFER_SIZE))
    s.close()
  except socket.error:
    degrev = " ----> Request OFF: Socket error occured <---- "
  return render_template('home.html', degre=degrev , ipserver=IP_SERVER)

# tells your operating system to listen on a public IP.
if __name__ == '__main__':
  app.run(host='0.0.0.0', port=5050)
